package net.diandangquan.processor;

import net.diandangquan.doc.XBiaoData;
import net.diandangquan.pipeline.XBiaoPipeLine;
import net.diandangquan.repository.XBiaoSpiderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Tiger
 * @date 2017/12/6 10:25
 */
@Component
public class XBiaoWatchProcessor implements PageProcessor {

    private Site site = Site.me().setRetrySleepTime(10000).setRetryTimes(1).setCharset("utf-8")
            .addHeader("Cookie",
                    "bdshare_firstime=1510655125535; PHPSESSID=ul9q3mf168c1ccmcae8lqhqot5; product_browser_cookid=151252601019882; Hm_lvt_a614a6a498e45b564870acd236f789d4=1512442474,1512443833,1512446242,1512526013; Hm_lpvt_a614a6a498e45b564870acd236f789d4=1512538981; CompareProducts=; show_type=99a105a112").setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");

    private String PAGE_LIST = "http://watch\\.xbiao\\.com/p\\d+\\.html";
    /**
     * 表款首页
     */
    private String PAGE_DETAIL_INDEX = "http://www\\.xbiao\\.com/[a-z]+/\\d+/";
    /**
     * 详细参数
     */
    private String PAGE_DETAIL_PARAM = "http://www\\.xbiao\\.com/[a-z]+/\\d+/param\\.html";
    /**
     * 图片
     */
    private String PAGE_DETAIL_PICTURES = "http://www\\.xbiao\\.com/[a-z]+/\\d+/pictures\\.html";
    /**
     * 报价
     */
    private String PAGE_DETAIL_PRICE = "http://www\\.xbiao\\.com/[a-z]+/\\d+/price/";

    @Autowired
    private XBiaoSpiderRepository spiderRepository;

    @Override
    public void process(Page page) {
        String pageUrl = page.getUrl().toString();
        //列表
        if (pageUrl.matches(PAGE_LIST)) {
            System.out.println("当前页码:----->"+pageUrl);
            List<Selectable> watchList = page.getHtml().xpath("//div[@class='watch left']/ul/li[1]/ul/li").nodes();
            watchList.forEach(selectable -> {
                String detailHref = selectable.xpath("//a/@href").toString();
                String xbiaoId = detailHref.replace("http://www.xbiao.com/", "");
//                page.putField("xbiaoId", xbiaoId);
                XBiaoData xBiaoData = new XBiaoData(xbiaoId);
                spiderRepository.save(xBiaoData);
                page.addTargetRequest(detailHref);
                page.addTargetRequest(detailHref + "param.html");
                page.addTargetRequest(detailHref + "pictures.html");
                page.addTargetRequest(detailHref + "price/");
            });
        }
        //单个表的表款首页
        else if (pageUrl.matches(PAGE_DETAIL_INDEX)) {
            String xbiaoId = pageUrl.replace("http://www.xbiao.com/", "");
            page.putField("xbiaoId", xbiaoId);
//            longines/7088/
            String image = page.getHtml().xpath("//div[@class='big-pic']/a/img/@src").toString();
            if(StringUtils.isEmpty(image)){
                image = page.getHtml().xpath("//div[@class='big-pic']/img/@src").toString();
            }
            page.putField("image", image);
            Pattern p = Pattern.compile("/\\d+/");
            Matcher m = p.matcher(xbiaoId);
            String brandNameEn = m.replaceAll("").trim();
            page.putField("brandNameEn", brandNameEn);
        }
        //详细参数
        else if (pageUrl.matches(PAGE_DETAIL_PARAM)) {
            page.putField("xbiaoId", pageUrl.replace("http://www.xbiao.com/", "").replace("param.html", ""));
            page.putField("nameFull",page.getHtml().xpath("//div[@class='wrapper']/h3/text()"));
            /*page.putField("oriUrl", pageUrl);
            page.putField("oriHtml", page.getHtml().xpath("//div[@class='param_info_box']"));
            page.putField("brandName", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[1]/td[2]/ul/li[2]/a/text()"));
            page.putField("xilei", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[1]/td[2]/ul/li[3]/a/text()"));
            page.putField("xinghao", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[1]/td[2]/ul/li[1]/text()"));
            page.putField("jixinleixin", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[1]/td[2]/ul/li[4]/a/text()"));
            page.putField("sex", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[1]/td[2]/ul/li[5]/a/text()"));

            //因为机芯的属性不确定，只能如此
            List<Selectable> liList = page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[2]/td[2]/ul/li").nodes();
            liList.forEach(selectable -> {
                String properties = selectable.xpath("//strong/text()").toString();
                if ("机芯型号：".equalsIgnoreCase(properties)) {
                    page.putField("jixinxinhao", selectable.xpath("//li/a/text()"));
                } else if ("出产厂商：".equalsIgnoreCase(properties)) {
                    page.putField("org", selectable.xpath("//li/text()"));
                } else if ("基础机芯：".equalsIgnoreCase(properties)) {
                    page.putField("jichujixin", selectable.xpath("//li/text()"));
                } else if ("机芯类型：".equalsIgnoreCase(properties)) {
                    page.putField("jixinleixin", selectable.xpath("//li/text()"));
                } else if ("机芯直径：".equalsIgnoreCase(properties)) {
                    page.putField("jixinzj", selectable.xpath("//li/text()"));
                } else if ("机芯厚度：".equalsIgnoreCase(properties)) {
                    page.putField("jixinhoudu", selectable.xpath("//li/text()"));
                } else if ("摆轮：".equalsIgnoreCase(properties)) {
                    page.putField("bailun", selectable.xpath("//li/text()"));
                } else if ("振频：".equalsIgnoreCase(properties)) {
                    page.putField("zhenping", selectable.xpath("//li/text()"));
                } else if ("宝石数：".equalsIgnoreCase(properties)) {
                    page.putField("baoshiNumber", selectable.xpath("//li/text()"));
                } else if ("零件数：".equalsIgnoreCase(properties)) {
                    page.putField("linjianNumber", selectable.xpath("//li/text()"));
                } else if ("游丝：".equalsIgnoreCase(properties)) {
                    page.putField("yousi", selectable.xpath("//li/text()"));
                } else if ("避震：".equalsIgnoreCase(properties)) {
                    page.putField("bizhen", selectable.xpath("//li/text()"));
                } else if ("动力储备：".equalsIgnoreCase(properties)) {
                    page.putField("donglichubei", selectable.xpath("//li/text()"));
                } else if ("电池寿命：".equalsIgnoreCase(properties)) {
                    page.putField("dichishoumin", selectable.xpath("//li/text()"));
                }
            });


            page.putField("biaojin", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[3]/td[2]/ul/li[1]/text()"));
            page.putField("biaokehoudu", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[3]/td[2]/ul/li[2]/text()"));
            page.putField("biaopangyanse", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[3]/td[2]/ul/li[3]/text()"));
            page.putField("biaopanxingzhuang", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[3]/td[2]/ul/li[4]/text()"));
            page.putField("biaodaiyanse", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[3]/td[2]/ul/li[5]/text()"));
            page.putField("biaokouleixing", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[3]/td[2]/ul/li[6]/text()"));
            page.putField("beitou", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[3]/td[2]/ul/li[7]/text()"));
            page.putField("weight", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[3]/td[2]/ul/li[8]/text()"));
            page.putField("fangshuishengdu", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[3]/td[2]/ul/li[9]/text()"));

            page.putField("biaokecaizhi", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[4]/td[2]/ul/li[1]/text()"));
            page.putField("biaopangcaizhi", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[4]/td[2]/ul/li[2]/text()"));
            page.putField("biaojingcaizhi", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[4]/td[2]/ul/li[3]/text()"));
            page.putField("biaoguancaizhi", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[4]/td[2]/ul/li[4]/text()"));
            page.putField("biaodaicaizhi", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[4]/td[2]/ul/li[5]/text()"));
            page.putField("biaokoucaizhi", page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[4]/td[2]/ul/li[6]/text()"));

            liList = page.getHtml().xpath("//div[@class='param_info_box']/table/tbody/tr[5]/td[2]/ul/li").nodes();
            StringBuffer sb = new StringBuffer();
            for (Selectable selectable : liList) {
                String gongneng = selectable.xpath("//strong/text()").toString();
                if ("该表款无其他特殊功能".equalsIgnoreCase(gongneng)) {
                    break;
                }
                sb.append(gongneng).append("，");
            }
            String gongnengStr = sb.toString();
            if (StringUtils.isNotEmpty(gongnengStr) && gongnengStr.length()>0) {
                gongnengStr = gongnengStr.substring(0, gongnengStr.length() - 1);
                page.putField("gongneng", gongnengStr);
            }*/
        }
        //图片
        else if (pageUrl.matches(PAGE_DETAIL_PICTURES)) {
            page.putField("xbiaoId", pageUrl.replace("http://www.xbiao.com/", "").replace("pictures.html", ""));

            List<Selectable> categoryImages = page.getHtml().xpath("//div[@class='pic_picContainer mar_t30']").nodes();
            Map<String, List<String>> map = new HashMap<>();
            categoryImages.forEach(selectable -> {
                String categoryName = selectable.xpath("//div[@class='title_box clearfix']/h3/text()").toString();
                List<String> imgList = new ArrayList<>();
                List<Selectable> images = selectable.xpath("//div[@class='pic_page_list']/ul/li").nodes();
                images.forEach(selectable1 -> {
                    String image = selectable1.xpath("//a/img/@src").toString();
                    imgList.add(image);
                });
                map.put(categoryName, imgList);
            });
            page.putField("images", map);
        }
        //报价
        else if (pageUrl.matches(PAGE_DETAIL_PRICE)) {
            page.putField("xbiaoId", pageUrl.replace("http://www.xbiao.com/", "").replace("price/", ""));

            page.putField("priceCn", page.getHtml().xpath("//div[@class='watch_info_box left']/dl/dd[1]/span/i/text()").toString());
            page.putField("priceCnTime", page.getHtml().xpath("//div[@class='watch_info_box left']/dl/dd[1]/em/text()").toString());
            page.putField("priceOy", page.getHtml().xpath("//div[@class='watch_info_box left']/dl/dd[2]/span/i/text()").toString());
            page.putField("priceOyTime", page.getHtml().xpath("//div[@class='watch_info_box left']/dl/dd[2]/em/text()").toString());
            page.putField("priceEn", page.getHtml().xpath("//div[@class='watch_info_box left']/dl/dd[3]/span/i/text()").toString());
            page.putField("priceEnTime", page.getHtml().xpath("//div[@class='watch_info_box left']/dl/dd[3]/em/text()").toString());
            page.putField("priceHk", page.getHtml().xpath("//div[@class='watch_info_box left']/dl/dd[4]/span/i/text()").toString());
            page.putField("priceHkTime", page.getHtml().xpath("//div[@class='watch_info_box left']/dl/dd[4]/em/text()").toString());
        }
        //总的页面（初始化 共1497页）
        else {
            for (int i = 1; i < 1498; i++) {
                page.addTargetRequest("http://watch.xbiao.com/p" + i + ".html");
            }
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new XBiaoWatchProcessor())
                .addPipeline(new ConsolePipeline())
//                .addPipeline(new XBiaoPipeLine())
                .addUrl("http://watch.xbiao.com/")
//                .addUrl("http://watch.xbiao.com/p2.html")
                .thread(50).run();
    }
}
